var tabSelected;

// 获取当前日期
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var today = year + "-" + month + "-" + day;

var hotPlayArgs = {today : today, type : "hotPlay", pageId : 1, pageSize : 8};
var hotPlayParam = {reqData : hotPlayArgs, reqUrl : "queryMovie"};

var notPlayArgs = {today : today, type : "notPlay", pageId : 1, pageSize : 8};
var notPlayParam = {reqData : notPlayArgs, reqUrl : "queryMovie"};

$(function()
{
	
	tabSelected = "正在热播";
	
	$("#main_left_middle").tabs(
	{
		plain : true,
		narrow : true
	});
	
	// easyui tabs事件
	$('#main_left_middle').tabs(
	{
	    /*onSelect : function(title, index)
	    {
	        alert(title + "=" + index);
	    }*/
	    
	    onSelect : tabSelect
	});

	hotPlayArgs.movieName = movieName;
	notPlayArgs.movieName = movieName;
	
	// 正在热播
	ajaxRequest(hotPlayParam, function(returnData)
	{
		if(returnData)
		{
			var page = returnData.page;
			hotPlayArgs.pageCount = page.pageCount;
			hotPlayArgs.dataCount = page.dataCount;
			
			var dataArr = returnData.data;
			
			showData(dataArr, "hotPlay");
		}
	}, null);
	
	// 即将上映
	ajaxRequest(notPlayParam, function(returnData)
	{
		if(returnData)
		{
			var page = returnData.page;
			notPlayArgs.pageCount = page.pageCount;
			notPlayArgs.dataCount = page.dataCount;
			
			var dataArr = returnData.data;
			
			showData(dataArr, "notPlay");
		}
	}, null);
});

// Tab切换
tabSelect = function(title, index)
{
	tabSelected = title;
}

// 显示数据
showData = function(dataArr, type)
{
	var dataCount = 0, pageCount = 0, pageId = 1;
	
	if(type == "hotPlay") // 正在热播
	{
		dataCount = parseInt(hotPlayArgs.dataCount);
		pageCount = parseInt(hotPlayArgs.pageCount);
		pageId = parseInt(hotPlayArgs.pageId);
	}
	else // 即将上映
	{
		dataCount = parseInt(notPlayArgs.dataCount);
		pageCount = parseInt(notPlayArgs.pageCount);
		pageId = parseInt(notPlayArgs.pageId);
	}
	
	if(dataCount == 0)
	{
		return;
	}
	
	var i = 0;
	var div = "<div>";

	for(var index in dataArr)
	{
		var data = dataArr[index];
		
		var movieId = data.movieId; // 影片ID
		var moviePhoto = data.moviePhoto; // 照片地址
		var movieName = data.movieName; // 影片名称
		var movieInfo = data.movieInfo; // 影片信息
		var movieActor = data.movieActor; // 影片演员
		var movieDate = data.movieDate.split(" ")[0]; // 上映日期
		var arr = movieDate.split("-");
		movieDate = arr[0] + "年" + arr[1] + "月" + arr[2] + "日";

		var photo = "<dt><a onclick=\"showDetails(" + movieId + ")\"><img src=\"" + moviePhoto + "\"/></a></dt>";
		var name = "<dd style=\"height: 15px;\"><a onclick=\"showDetails(" + movieId + ")\"><b>" + movieName + "</b></a></dd>";
		var date = "<dd style=\"height: 15px;\"><b>" + movieDate + "</b></dd>";
		var info = "<dd style=\"height: 30px;\">" + movieInfo + "</dd>";
		var actor = "<dd style=\"height: 30px;\">" + movieActor + "</dd>";
		var buy = "<dd style=\"height: 24px;\"><input type=\"button\" value=\"我要购票\" style=\"float: right;\" onclick=\"showDetails(" + movieId + ")\"></dd>";
		var dl = "<dl>" + photo + name + date + info + actor + buy + "</dl>";

		div += dl;
		
		if(i == 3)
		{
			div += "</div><div>";
			i = 0;
		}
		
		i ++;
	}
	
	div += "</div>";

	var page = "";
	
	if(pageCount > 1)
	{
		var eachNum = pageCount >= 4 ? 4 : pageCount;
		var id=pageId;
		var prePageId = pageId - 1;
		prePageId = prePageId == 0 ? 1 : prePageId;
		var temp = prePageId + 3;
		
		if(eachNum == 4)
		{
			if(pageId == pageCount)
			{
				prePageId = pageId - 3;
			}
			else
			{
				if(temp > pageCount)
				{
					prePageId --;
				}
			}
		}
		else
		{
			prePageId = 1;
		}

		var arr = [];
		
		for(var i=0; i<eachNum; i++)
		{
			var pageNum = prePageId + i;
			var a = "<a onclick=\"paging(" + pageNum + ")\"><" + pageNum + "></a>&nbsp;";
			
			if(pageNum == pageId)
			{
				a = "<a onclick=\"paging(" + pageNum + ")\">" + pageNum + "</a>&nbsp;";
			}
			
			arr.push(a);
		}
		
		if(id==1){

			page = "<div style=\"width: 100%; clear: both;\"></div><div style=\"margin-left: 320px;\">" + arr.join("") + "<a onclick=\"paging(" + pageId + "+1)\">" + "下一页" + "</a>&nbsp;" + "</div>";
			
		}else if(id>=pageCount){
			page = "<div style=\"width: 100%; clear: both;\"></div><div style=\"margin-left: 280px;\">"+ "<a onclick=\"paging(" + pageId + "-1)\">" + "上一页" + "</a>&nbsp;" + arr.join("") + "</div>";
		}else{
			page = "<div style=\"width: 100%; clear: both;\"></div><div style=\"margin-left: 280px;\">"+ "<a onclick=\"paging(" + pageId + "-1)\">" + "上一页" + "</a>&nbsp;" + arr.join("")+ "<a onclick=\"paging(" + pageId + "+1)\">" + "下一页" + "</a>&nbsp;" + "</div>";
		}
	}

	div += page;
	
	if(type == "hotPlay") // 正在热播
	{
		$("#divHot").html(div);
	}
	else
	{
		$("#divStart").html(div);
	}
}

// 分页
paging = function(pageId)
{
	if(tabSelected == "正在热播")
	{
		hotPlayArgs.pageId = pageId;

		ajaxRequest(hotPlayParam, function(returnData)
		{
			if(returnData)
			{
				var page = returnData.page;
				hotPlayArgs.pageCount = page.pageCount;
				hotPlayArgs.dataCount = page.dataCount;
				
				var dataArr = returnData.data;
				
				showData(dataArr, "hotPlay");
			}
		}, null);
	}
	else
	{
		notPlayArgs.pageId = pageId;
		
		ajaxRequest(notPlayParam, function(returnData)
		{
			if(returnData)
			{
				var page = returnData.page;
				notPlayArgs.pageCount = page.pageCount;
				notPlayArgs.dataCount = page.dataCount;

				var dataArr = returnData.data;

				showData(dataArr, "notPlay");
			}
		}, null);
	}
}

// 影片相关详情
showDetails = function(movieId)
{
	window.location = "singleMovie?movieId=" + movieId;
}